<template>
	<view class="unlocking">
		<view class="unlocking-title">
			<view class="row">
				<image src="../../static/images/store.png" mode=""></image>
				<text>{{info.room.room_name}}-{{info.store.store_name}}</text>
			</view>
			<view class="row">
				<image src="/static/images/calender.png" mode=""></image>
				<text>{{moment(info.starttime).subtract(vuex_config.unlock_inadvance_min, 'm').format('MM/DD HH:mm')}} - {{moment(info.endtime).format('HH:mm')}}
				</text>
			</view>
		</view>
		<view class="unlocking-tabs">
			<view class="unlocking-tabs-g">
				<u-line direction="col" length="72rpx"></u-line>
			</view>
			<view class="unlocking-tabs-con">
				<u-tabs :list="list1" lineWidth="60rpx" lineColor="#40C6A1" lineHeight='12rpx'
					:activeStyle="activeStyle" line='border-radius: 5rpx;' :inactiveStyle="inactiveStyle"
					itemStyle="padding-left: 60rpx; padding-right: 60rpx;padding-bottom:25rpx; height: 34px;"
					v-model="current" @click="handleClick"></u-tabs>
				<view v-if="current === 0">
					<!-- start  蓝牙开锁成功或者失败的样式 -->
					<view class="open-res" >
						<view v-if="lockMessage">
							<view class="item" v-if="'开锁成功' == lockMessage">
								<img src="../../static/images/success.png" alt="">
								开锁成功
							</view>
							<view class="item" v-else>
								<img src="../../static/images/error.png" alt="">
								{{ lockMessage }}
							</view>
						</view>
					</view>

					<!-- end  蓝牙开锁成功或者失败的样式 -->
					<view class="btnlocking" @click="blueopendoor">
						点击开锁
					</view>
				</view>
				<view class="lock-code" v-if="current === 1">
					{{info.tts_randkeypwd}}
				</view>
			</view>
		</view>
		<view class="lock-code-all">
			<view class="lock-code-call" @click="makePhoneCall(info.store.store_mobile)">
				<image src="../../static/images/tel.png" mode=""></image>
				<view class="">
					紧急电话
				</view>
			</view>
			<view class="btnlocking-attentionS">
				<view class="btnlocking-attention-tit">
					<image src="../../static/images/tips.png" mode=""></image>
					<text>注意事项</text>
				</view>
				<view class="btnlocking-attention-text">1. 蓝牙开锁按钮在生效期可点击 </view>
				<view class="btnlocking-attention-text">2. 点击开锁前，先触摸“按键区”激活锁</view>
			</view>
		</view>
	</view>
</template>

<script>
	import moment from 'moment'
	const tongtongsuo = requirePlugin('tongtongsuo');
	import {
		get_reservation_details_api,
	} from '@/common/api.js'
	export default {
		data() {
			return {
				moment: moment,
				activeStyle: {
					color: '#303133',
					fontSize: '36rpx',
					fontWeight: 'bold',
					transform: 'scale(1.05)',
				},
				inactiveStyle: {
					color: '#606266',
					transform: 'scale(1)',
					borderRadius: '5rpx',
				},
				current: 0,
				list1: [{
					name: '蓝牙开锁',
				}, {
					name: '密码开锁',
				}, ],
				info: {
					tts_bluelockdata: '',
					tts_randkeypwd: '',
					store: {
						store_name: ''
					},
					room: {
						room_name: ''
					}
				},
				lockMessage: '',
			};
		},
		onLoad({
			id
		}) {
			this.getDetail(id)
		},
		methods: {
			getDetail(id) {
				get_reservation_details_api({
					id
				}).then(({
					data
				}) => {
					this.info = data
					const start = moment(data.starttime).subtract(this.vuex_config.unlock_inadvance_min, 'm')
						.format(
							'YYYY-MM-DD HH:mm:ss')
					console.log(start);
					const nowDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
					if (typeof data.tts_randkeypwd != 'string') {
						data.tts_randkeypwd = data.tts_randkeypwd.toString()
					}
					if (moment(nowDate).valueOf() > moment(start).valueOf() && moment(nowDate).valueOf() < moment(
							data
							.endtime)
						.valueOf()) {
						data.tts_randkeypwd = data.tts_randkeypwd
						data['type'] = 1
					} else {
						data.tts_randkeypwd = data.tts_randkeypwd.replace(/^[0-9]*$/, new Array(data
							.tts_randkeypwd.length).fill('*').join(''))
						data['type'] = 0
					}
				})
			},
			blueopendoor() {
				if (!this.info.type) return
				wx.showLoading({
					title: "正在开启智能锁",
				})
				const start = Date.now();
				// 调用开锁接口
				tongtongsuo.controlLock(tongtongsuo.ControlAction.OPEN, this.info.tts_bluelockdata, res => {
					debugger
					if (res.errorCode === 10003) {
						console.log("获取版本信息时设备连接已断开", res)
					}
				}, null, this.deviceId).then(res => {
					wx.hideLoading({});

					if (res.errorCode === 0) {
						this.lockMessage = '开锁成功'
					} else {
						this.lockMessage = `开锁失败 ${res.errorMsg}`
					}
					this.info.type = 2
					this.$forceUpdate()
				})
			},
			handleClick(item) {
				this.current = item.index
			}
		}
	}
</script>

<style lang="scss" scoped>
	.unlocking {
		box-sizing: border-box;
		padding: 0 24rpx;

		.unlocking-title {
			width: 702rpx;
			height: 200rpx;
			background: #F3F4F6;
			margin-top: 32rpx;
			overflow: hidden;
			margin-bottom: 50rpx;
			border-radius: 16rpx 16rpx 16rpx 16rpx;

			.row {
				display: flex;
				align-items: center;
				margin: 24rpx;

				image {
					width: 64rpx;
					height: 64rpx;
					margin-right: 16rpx;
					flex-shrink: 0;
				}

				text {
					font-size: 32rpx;
					font-weight: 500;
					color: #606266;
					flex: 1;
					white-space: nowrap;
				}
			}
		}

		.unlocking-tabs {}
	}

	.btnlocking {
		width: 276rpx;
		height: 276rpx;
		background: #ECFDF8;
		border-radius: 50%;
		text-align: center;
		line-height: 276rpx;
		margin: 80rpx auto 0rpx auto;
		font-size: 36rpx;
		font-weight: 500;
		color: #106D54;
		box-shadow: 0rpx 4rpx 8rpx 0rpx #C0C4CC;
	}

	.emergency-call {
		width: 104rpx;
		height: 200rpx;
		background: #F3F4F6;
		border-radius: 100rpx 100rpx 100rpx 100rpx;
		opacity: 1;
		text-align: center;
		border: 2rpx solid #C0C4CC;
		position: fixed;
		// margin-top: 158rpx;
		right: 24rpx;
		overflow: hidden;

		image {
			width: 64rpx;
			height: 64rpx;
			margin-top: 26rpx;
		}

		view {
			width: 48rpx;
			height: 80rpx;
			font-size: 24rpx;
			font-weight: 300;
			margin-top: 14rpx;
			color: #909399;
			margin-left: 28rpx;
		}
	}

	.btnlocking-attention {
		// margin-top: 412rpx;
		margin-left: -90rpx;

		.btnlocking-attention-tit {
			display: flex;
			margin-bottom: 24rpx;

			image {
				width: 36rpx;
				height: 36rpx;
				margin-right: 16rpx;
			}

			text {
				font-size: 28rpx;
				font-weight: 400;
				color: #606266;
			}
		}

		.btnlocking-attention-text {
			font-size: 24rpx;
			font-weight: 300;
			color: #909399;
			line-height: 40rpx;
		}
	}

	.btnlocking-attentionS {
		margin-top: 42rpx;

		.btnlocking-attention-tit {
			display: flex;
			margin-bottom: 24rpx;

			image {
				width: 36rpx;
				height: 36rpx;
				margin-right: 16rpx;
			}

			text {
				font-size: 28rpx;
				font-weight: 400;
				color: #606266;
			}
		}

		.btnlocking-attention-text {
			font-size: 24rpx;
			font-weight: 300;
			color: #909399;
			line-height: 40rpx;
		}
	}

	.lock-code {
		margin-top: 296rpx;
		font-size: 64rpx;
		font-weight: 500;
		height: 90rpx;
		text-align: center;
		color: #000000;
	}

	.unlocking-tabs-g {
		position: absolute;
		left: 360rpx;

	}

	.lock-code-all {
		position: fixed;
		bottom: 74rpx;
		left: 0rpx;
		width: 100%;
		padding: 0rpx 24rpx;
		box-sizing: border-box;

		.lock-code-call {
			width: 104rpx;
			height: 200rpx;
			background: #F3F4F6;
			border-radius: 100rpx 100rpx 100rpx 100rpx;
			opacity: 1;
			text-align: center;
			border: 2rpx solid #C0C4CC;
			position: fixed;
			right: 24rpx;
			overflow: hidden;
			bottom: 270rpx;
			z-index: 10;

			image {
				width: 64rpx;
				height: 64rpx;
				margin-top: 26rpx;
			}

			view {
				width: 48rpx;
				height: 80rpx;
				font-size: 24rpx;
				font-weight: 300;
				margin-top: 14rpx;
				color: #909399;
				margin-left: 28rpx;
			}
		}
	}

	::v-deep .u-tabs__wrapper__nav {
		width: 544rpx;
		margin: 0rpx auto;
		// justify-content: center !important;
	}

	.open-res {
		margin-top: 66rpx;
		height: 48rpx;
		.item {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32rpx;
			color: #606266;
		}

		img {
			width: 48rpx;
			height: 48rpx;
			margin-right: 24rpx;
		}
	}
</style>
